package com.example.cadmin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.cadmin.entity.AccessLogs;
import com.example.cadmin.entity.BorrowingRecords;
import com.example.cadmin.service.*;
import com.example.cadmin.utils.ResponseModel;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zzsoft
 * @since 2023-01-09
 */
@Controller
@RequestMapping("/file/log")
public class FileLogController {
    @Resource
    private MyUserService myUserService;
    @Resource
    private FileLogService fileLogService;
    @Resource
    public HostService hostService;
    @Resource
    public AccessLogsService accessLogsService;
    @Resource
    public BorrowingRecordsService borrowingRecordsService;


    /**
     * 档案操作记录页面
     */
    @GetMapping("index")
    public ModelAndView historyRecordLog() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("FileLog/historyRecordLog");
        List<Map<String, Object>> hostList = hostService.listMaps();
        modelAndView.addObject("hostList", hostList);
        return modelAndView;
    }

    /**
     * 档案操作管理list接口
     *
     * @param page
     * @param limit
     */
    @GetMapping("list")
    @ResponseBody
    public Map<String, Object> fileLogList(@RequestParam(value = "page") int page,
                                           @RequestParam(value = "limit") int limit,
                                           @RequestParam(value = "startDate",required = false) String startDate,
                                           @RequestParam(value = "endDate",required = false) String endDate,
                                           @RequestParam(value = "fileName",required = false) String fileName,
                                           @RequestParam(value = "borrowingType",required = false) String borrowingType,
                                           @RequestParam(value = "number",required = false) String number) {
        return fileLogService.fileLogList(page, limit,startDate,endDate,fileName,borrowingType,number);
    }

    /**
     * 清空日志
     */
    @PostMapping("clear")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('filelog:clear')")
    public ResponseModel clearLog() {
        borrowingRecordsService.remove(new QueryWrapper<>());
        return ResponseModel.success("日志已清空");
    }
}
